package com.lepu.app.fun.index.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lepu.app.fun.my.bean.BeanMyArticle;
import com.lepu.pasm.R;

public class IndexKnowAdapter extends BaseAdapter {

	private LayoutInflater layoutInflater = null;
	private Context context = null;
	private ArrayList<BeanMyArticle> knowList;
	private int selectIndex;
	
	public IndexKnowAdapter(Context context , ArrayList<BeanMyArticle> knowList, int selectIndex) {
		this.context = context;
		this.knowList = knowList;
		this.selectIndex = selectIndex;
		layoutInflater = LayoutInflater.from(context);
		
	}

	@Override
	public int getCount() {
		
		return knowList.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint({ "InflateParams", "NewApi" })
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		final ViewHolder holder;

		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.index_know_gridview_item, null);

			holder = new ViewHolder();
			LinearLayout itemLayout = (LinearLayout) convertView.findViewById(R.id.know_item_layout);
			TextView noTextView = (TextView) convertView.findViewById(R.id.know_item_no);
			TextView dayTextView = (TextView) convertView.findViewById(R.id.know_item_day);
			TextView dateTextView = (TextView) convertView.findViewById(R.id.know_item_date);
			TextView lookTextView = (TextView) convertView.findViewById(R.id.know_item_look);
			TextView favorTextView = (TextView) convertView.findViewById(R.id.know_item_favor);

			holder.itemLayout = itemLayout;
			holder.noTextView = noTextView;
			holder.dayTextView = dayTextView;
			holder.dateTextView = dateTextView;
			holder.lookTextView = lookTextView;
			holder.favorTextView = favorTextView;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
        
		BeanMyArticle item = knowList.get(position);
		
		holder.dayTextView.setText(item.ArticleTitle.substring(1, knowList.get(position).ArticleTitle.length()-1));
		holder.dateTextView.setText(item.ArticleTitle.substring(knowList.get(position).ArticleTitle.length()-1, knowList.get(position).ArticleTitle.length()));
		holder.lookTextView.setText("阅读"+item.ViewCount);
		holder.favorTextView.setText("点赞"+item.FavourCount+"");

		if(position == selectIndex){
			holder.itemLayout.setBackgroundResource(R.drawable.index_know_item_select_icon);
			holder.noTextView.setTextColor(context.getResources().getColor(R.color.color_ff5d1d));
			holder.dayTextView.setTextColor(context.getResources().getColor(R.color.color_ff5d1d));
			holder.dateTextView.setTextColor(context.getResources().getColor(R.color.color_ff5d1d));
		}else{
			holder.itemLayout.setBackgroundResource(R.drawable.index_know_item_icon);
			holder.noTextView.setTextColor(context.getResources().getColor(R.color.color_333));
			holder.dayTextView.setTextColor(context.getResources().getColor(R.color.color_black));
			holder.dateTextView.setTextColor(context.getResources().getColor(R.color.color_333));
		}
		
		return convertView;
	}
	
	static class ViewHolder {
		LinearLayout itemLayout;
		TextView noTextView;
		TextView dayTextView;
		TextView dateTextView;
		TextView lookTextView;
		TextView favorTextView;
	}
}
